<?php if (count($events_by_day) == 0): ?>
<p>Nenhum evento cadastrado.</p>
<?php endif; ?>

<?php
  // Create control array
  $printed_events = array();
?>

<?php foreach ($events_by_day as $day => $events): ?>
<?php
  // Initialize usefull variables 
  $daytime = strtotime($day);
  $yearday = date('z', $daytime);

  // Separate multiple day and single day events
  $multiple_day_events = array();
  $single_day_events = array();
  foreach ($events as $event)
  {
    if ($event->getEndDate() == null || 
        $event->getEndDate('z') == $event->getStartDate('z'))
      $single_day_events[] = $event;
    else
      $multiple_day_events[] = $event;
  }
?>
  <?php if(count($single_day_events) > 0) : ?>
    <?php if(!$more_headers) : ?>
  <table class="events">
    <?php else : ?>
  <table class="events_long">
    <?php endif; ?>  
    <thead>
      <tr>
        <th>Evento<?php if (count($single_day_events) > 1) echo 's'; ?>&nbsp;-&nbsp;<?php
       if ($day == date('Y-m-d'))
         echo 'Hoje';
       elseif($day == date('Y-m-d', time()+(24*60*60)))
         echo 'Amanh&atilde;';
       elseif(date('Y') == date('Y', $daytime) && $yearday - date('z') < 7)
         echo htmlentities(ucfirst(strftime('%A',$daytime))).' ('.date('d/m/Y', $daytime).')';
       else
         echo date('d/m/Y', $daytime);
    ?></th>
    <?php if ($more_headers) : ?>
        <th class="location">Local</th>
    <?php endif; ?>
        <th class="category">Categoria</th>
      </tr>
    </thead>
    <tbody>
    <?php foreach($single_day_events as $event): ?>
      <?php $printed_events[] = $event; ?>
      <tr>
        <td><a class="ajaxLink" href="<?php echo url_for('event_show', $event) ?>"><?php echo $event->getDescription() ?></a></td>
      <?php if($more_headers) : ?>
        <td><?php echo $event->getLocation() ?></td>
      <?php endif; ?>
        <td>
      <?php echo $event->getBolaoCategory()->getName() ?>
      <?php
         if ($event->hasBolaoSubcategory())
           echo ' ('.$event->getBolaoSubcategory()->getName().')';
      ?>
        </td>
      </tr>
    <?php endforeach; ?>
    </tbody>
  </table>
  <?php endif; ?>

  <?php if(count($multiple_day_events) > 0) : ?>
    <?php foreach ($multiple_day_events as $event) : ?>
      <?php
         if ($event->getStartDate('z') != $yearday &&
             !($event->getStartDate('m') != date('m', $daytime) && 
               date('j',$daytime) == 1))
           continue;
         if (in_array($event, $printed_events))
           continue;

         $printed_events[] = $event;
      ?>
      <?php if(!$more_headers) : ?>
  <table class="events">
      <?php else : ?>
  <table class="events_long">
      <?php endif; ?>  
    <thead>
      <tr>
        <th>Evento - 
      <?php
         if ($event->getStartDate('m') != $event->getEndDate('m'))
           $datestring = $event->getStartDate('d/m');
         else
           $datestring = $event->getStartDate('d');
         $datestring .= ' a '.$event->getEndDate('d/m/Y');
         if ($day == date('Y-m-d'))
           echo 'Hoje ('.$datestring.')';
         elseif($day == date('Y-m-d', time()+(24*60*60)))
           echo 'Amanh&atilde; ('.$datestring.')';
         elseif(date('Y') == date('Y', $daytime) && $yearday - date('z') < 7)
           echo htmlentities(ucfirst(strftime('%A',$daytime))).' ('.$datestring.')';
         else
           echo $datestring;
      ?>
        </th>
      <?php if ($more_headers) : ?>
        <th class="location">Local</th>
      <?php endif; ?>
        <th class="category">Categoria</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><a class="ajaxLink" href="<?php echo url_for('event_show', $event) ?>"><?php echo $event->getDescription() ?></a></td>
        <?php if($more_headers) : ?>
        <td><?php echo $event->getLocation() ?></td>
        <?php endif; ?>
        <td>
        <?php echo $event->getBolaoCategory()->getName() ?>
        <?php
           if ($event->hasBolaoSubcategory())
             echo ' ('.$event->getBolaoSubcategory()->getName().')';
        ?>
        </td>
      </tr>
    </tbody>
  </table>
    <?php endforeach; ?>
  <?php endif; ?>
<?php endforeach; ?>
